package com.home.school.controller;

import com.home.school.bean.TOrder;
import com.home.school.bean.TSubject;
import com.home.school.bean.TTeacherSubject;
import com.home.school.constant.OrderStatus;
import com.home.school.service.TOrderService;
import com.home.school.service.TSubjectService;
import com.home.school.service.TTeacherSubjectService;
import com.home.school.util.DateUtil;
import com.home.school.vo.*;
import com.home.school.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/order")
@RestController
public class OrderController {

    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private TSubjectService tSubjectService;

    @Autowired
    private TTeacherSubjectService tTeacherSubjectService;

    /**
     * 用户发起 订单，保存订单信息
     * @param userVO
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderSaveVO userVO) {
        // 课程编号查询课程
        String num = userVO.getNum();
        TSubject tSubject = tSubjectService.findByNum(num);
        TTeacherSubject tTeacherSubject = tTeacherSubjectService.findBySubjectId(num);
        TOrder tOrder = new TOrder();
        tOrder.setTime(DateUtil.strToDate(userVO.getTime(),DateUtil.YYYY_MM_DD_HH_MM_SS));
        tOrder.setStatus(OrderStatus.INIT.getStatus());
        tOrder.setAddress(userVO.getAddress());
        tOrder.setTeaId(tTeacherSubject.getTeaId());
        tOrder.setStuId(userVO.getStuId());
        tOrder.setSubjectId(tSubject.getId());
        tOrderService.save(tOrder);
        return R.ok();
    }

    /**
     * 修改订单
     * @param userVO
     * @return
     */
    @RequestMapping("modify")
    public R modify(@RequestBody TOrder userVO){
        tOrderService.updateById(userVO);
        return R.ok();
    }

    /**
     * id 查询
     * @param id
     * @return
     */
    @RequestMapping("byId/{id}")
    public R byId(@PathVariable("id") String id){
        return R.ok().data(tOrderService.getById(id));
    }
    /**
     * id 删除
     * @param id
     * @return
     */
    @RequestMapping("deleted/{id}")
    public R deleted(@PathVariable("id") Integer id){
        return R.ok().data(tOrderService.removeById(id));
    }

    /**
     * 用户订单列表
     * @param userListVO
     * @return
     */
    @RequestMapping("getUserList")
    public R getListUrl(@RequestBody OrderList userListVO){
        return tOrderService.userList(userListVO);
    }

    /**
     * 教师订单列表
     * @param teaOrderListVO
     * @return
     */
    @RequestMapping("getTeaList")
    public R getTeaList(@RequestBody OrderList teaOrderListVO){
        return tOrderService.teaList(teaOrderListVO);
    }

    /**
     * 确认订单
     * @param orderConfirmVO
     * @return
     */
    @RequestMapping("confirm")
    public R confirm(@RequestBody OrderConfirmVO orderConfirmVO){
        return tOrderService.confirm(orderConfirmVO);
    }

    /**
     * 取消 订单
     * @param orderConfirmVO
     * @return
     */
    @RequestMapping("cancel")
    public R cancel(@RequestBody OrderConfirmVO orderConfirmVO){
        return tOrderService.cancel(orderConfirmVO);
    }

    /**
     * 完成订单
     * @param orderConfirmVO
     * @return
     */
    @RequestMapping("over")
    public R over(@RequestBody OrderConfirmVO orderConfirmVO){
        return tOrderService.over(orderConfirmVO);
    }

    /**
     * 拒绝订单
     * @param orderConfirmVO
     * @return
     */
    @RequestMapping("reject")
    public R reject(@RequestBody OrderConfirmVO orderConfirmVO){
        return tOrderService.reject(orderConfirmVO);
    }
    /**
     * 订单列表
     * @param teaOrderListVO
     * @return
     */
    @RequestMapping("getList")
    public R getList(@RequestBody OrderList teaOrderListVO){
        return tOrderService.orderList(teaOrderListVO);
    }

}
